import * as types from "@/common/vuex.const";
import _ from "lodash";

let singleChatData = {};
let groupChatData = {};
let msgCounter = {};
try {
    const res = uni.getStorageInfoSync();
    if (!_.isEmpty(res)) {
        for (const key of res.keys) {
            if (key.startsWith(types.SINGLE_CHAT_DATA_PREFIX)) {
                //单聊消息
                singleChatData[key.split(":")[1]] = uni.getStorageSync(key);
            } else if (key.startsWith(types.GROUP_CHAT_DATA_PREFIX)) {
                //群聊消息
                uni.getStorage({
                    key: key,
                    success: function (res) {
                        groupChatData[key] = JSON.parse(res)
                    }
                });
            }
        }
    }

    //离线消息计数器
    let msgCounterJson = uni.getStorageSync(types.OFFLINE_MSG_COUNTER_DATA);
    if (!_.isEmpty(msgCounterJson)) {
        msgCounter = JSON.parse(msgCounterJson)
    }

} catch (e) {
}


export default {
    namespaced: true,
    state: {
        vuex_single_chat_msgs: singleChatData,
        vuex_group_chat_msgs: groupChatData,
        vuex_offline_msg_counter: msgCounter
    },
    mutations: {
        [types.ADD_SINGLE_CHAT]: (state, msg) => {
            let key = types.SINGLE_CHAT_DATA_PREFIX + msg.searchId;
            let msgs = state.vuex_single_chat_msgs[key];
            if (!msgs) {
                msgs = [];
                state.vuex_single_chat_msgs[key] = msgs;
            }
            let msgClone = _.assign(msg,{});
            //重复的不加入
            if (isRepeatSingleMsg(msgs, msgClone)) {
                return;
            }
            lruPushSingleMsg(msgs, msgClone);
            let json = JSON.stringify(msgs);
            uni.setStorage({
                key: key,
                data: json,
                success: function () {
                    console.log('success');
                }
            });
        },
        //更新离线消息计数器
        [types.UPDATE_OFFLINE_MSG_COUNTER]: (state, msgList) => {
            for (const key in msgList) {
                let val = msgList[key];
                let ele = state.vuex_offline_msg_counter[key];
                if (_.isEmpty(ele)) {
                    ele = val;
                } else {
                    ele.count = ele.count + val.count;
                    ele.timestamp = val.timestamp;
                    ele.lastMsg = val.lastMsg;
                }
                state.vuex_offline_msg_counter[key] = ele;
            }
            uni.setStorage({
                key: types.OFFLINE_MSG_COUNTER_DATA,
                data: JSON.stringify(state.vuex_offline_msg_counter),
                success: function () {
                    console.log('success');
                }
            });
        }
    },
    getters: {
        messageCounter: (state) => {
            const result = [];
            for (const key in state.vuex_offline_msg_counter) {
                result.push(state.vuex_offline_msg_counter[key])
            }
            result.sort((a, b) => {
                return b.timestamp - a.timestamp
            });
            return result;
        },
        singleChatList: (state) => (searchId, pageSize, lastTimestamp) => {
            let msgs = state.vuex_single_chat_msgs[searchId];
            if (_.isEmpty(msgs)) {
                return [];
            }
            let result;
            //没有lastTimestamp说明是第一页
            if (_.isEmpty(lastTimestamp)) {
                result = msgs.slice(0, pageSize);
            } else {
                result = msgs.filter(ele => {
                    return ele.sendTimestamp < lastTimestamp;
                }).slice(0, pageSize);
            }
            return result;
        }
    }
};

function isRepeatSingleMsg(msgs, msg) {
    for (const item of msgs) {
        if (item) {
            if (item.sequence == msg.sequence) {
                return true;
            }
        }
    }
    return false;
}

function lruPushSingleMsg(msgs, msg) {
    msgs.push(msg);
    if (msgs.length > 200) {
        msgs.pop();
    }
}




